#include "widget.h"
#include "ui_widget.h"

Robot *Widget::_Robot = nullptr;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //
    if(_Robot == nullptr){
        assembeRobot();
        _Robot = createRobot();
    }

    //test manager

    QString usr;
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_001");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_002");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_003");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_004");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_005");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_006");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_007");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_008");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_009");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_010");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_011");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_012");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_013");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_014");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_015");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_016");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_017");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_018");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_019");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_020");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_021");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_022");
    Manager::GetInstance().AddAuthority(LEVEL3,"OBJ_023");

    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_001");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_002");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_003");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_004");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_005");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_006");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_007");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_008");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_009");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_010");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_011");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_012");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_013");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_014");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_015");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_016");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_017");
//    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_018");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_019");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_020");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_021");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_022");
    Manager::GetInstance().AddAuthority(LEVEL2,"OBJ_023");

//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_001");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_002");
    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_003");
    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_004");
    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_005");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_006");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_007");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_008");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_009");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_010");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_011");
    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_012");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_013");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_014");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_015");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_016");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_017");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_018");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_019");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_020");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_021");
//    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_022");
    Manager::GetInstance().AddAuthority(LEVEL1,"OBJ_023");


    Manager::GetInstance().SetCheckUse("test");
    Manager::GetInstance().CheckSetting("abcd");
    Manager::GetInstance().CheckSetting("abcd","test");

    Manager::GetInstance().AddUser(LEVEL1,"test","12345");
    Manager::GetInstance().AddUser(LEVEL1,"test2","OBJ_023");
    Manager::GetInstance().AddUser(LEVEL1,"test3","1q2w3e");
    Manager::GetInstance().AddUser(LEVEL1,"test4","1q2w3e");
    Manager::GetInstance().AddUser(LEVEL2,"test5","1q2w3e");
    Manager::GetInstance().AddUser(LEVEL3,"test6","1q2w3e");

    Manager::GetInstance().CheckSetting("OBJ_023","test");
    Manager::GetInstance().CheckSetting("OBJ_022","test");

    Manager::GetInstance().CheckSetting("abcd");

    Manager::GetInstance().CheckUserPwd("test","12345");
    Manager::GetInstance().GetUser(LEVEL3,usr);
    Manager::GetInstance().DelUser("test");

    qDebug() << usr;



}

Widget::~Widget()
{
    delete ui;
}


void Widget::assembeRobot()
{
    int nRobotId = ID_ROBOT_BASE;

    //QString strRobotFilePath = QApplication::applicationDirPath().append("/robot");
    QString strRobotFilePath = QApplication::applicationDirPath();
    qDebug()<<strRobotFilePath;
    QDir dir(strRobotFilePath);
    QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
    if(fileInfoList.size()==0)
        //LogOutput::GetInstance().sendLogToView("RobotFactory", EnumLogTypeWarning, QCoreApplication::tr("目录(")+strRobotFilePath+QCoreApplication::tr("):无dll!"));
    qDebug() << "目录无dll!";
    qDebug() << fileInfoList.size();
    foreach (auto fileInfo, fileInfoList) {
      if(fileInfo.isFile())
      {
          QString strPluginPath = fileInfo.absoluteFilePath();
          qDebug() << __FUNCTION__ << __LINE__ << "  : " << strPluginPath;
          if(fileInfo.fileName().left(9)!="CarPosHMS")
              continue;

          _myLib.setFileName(strPluginPath);

          if(_myLib.load())
          {
              pDllGetPluginRobotVector pFuncGetVector = (pDllGetPluginRobotVector)_myLib.resolve("DllGetPluginRobotVector");
              if(pFuncGetVector)
              {
                  VecRobotInfo* pVecRobotInfo = (VecRobotInfo *)pFuncGetVector();
                  if(!pVecRobotInfo)
                      //LogOutput::GetInstance().sendLogToView("RobotFactory", EnumLogTypeError, QCoreApplication::tr("获取VecRobotInfo对象失败"));
                      qDebug() << "获取VecRobotInfo对象失败";
                  else
                  {
                      std::for_each(pVecRobotInfo->begin(), pVecRobotInfo->end(), [&](auto& v){
                          v.robotType.robotId = nRobotId++;
                          m_vecRobotInfo.push_back(v);
                          qDebug() << __FUNCTION__ << v.robotType.robotId << v.robotType.strRobotType ;
                  });

                  }

              }
              else{
                  qDebug() << "获取DllGetPluginRobotVector接口失败";
                  QMessageBox::about(this,"加载失败:","获取DllGetPluginRobotVector接口失败");
              }
          }
          else{
              qDebug() << "加载失败:" << fileInfo.fileName();
              QMessageBox::about(this,"加载失败:",fileInfo.fileName());
         }
      }
    }

}

Robot *Widget::createRobot()
{
    Robot* pRobot = nullptr;
    qDebug() << m_vecRobotInfo.size();
    auto it = m_vecRobotInfo.begin();

    qDebug() << __FUNCTION__;
    if(it!=m_vecRobotInfo.end())
    {
        pRobot = it->pFuncCreateRobot();
        if(pRobot){
            connect(pRobot,&Robot::sendToVirsual,this,&Widget::ReceiveRobot);
            pRobot->start();
                //pRobot->setRobotType(it->robotType);
        }
    }

    return pRobot;
}

void Widget::ReceiveRobot(unsigned char* data, int nLength)
{
    qDebug() << __FUNCTION__;
    std::string str;
    str.append((char*)data,nLength);
    QMessageBox::about(this,"ReceiveRobot:",QString(str.c_str()));
    emit SendToVisual(data, nLength);
}
void Widget::ReceiveVisual(unsigned char* data, int nLength)
{
    qDebug() << __FUNCTION__;
    if(_Robot != nullptr)
        _Robot->onReceiveFormVirsual((char*)data,nLength);
}
